﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.Api.Write.GetCelim;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodsslaughterAddCommandHandler : IRequestHandler<GoodsslaughterAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodsslaughterInfo> _slaughterbaseRepository;
        private readonly IIdContextCemil _iidContextCemil;
        public GoodsslaughterAddCommandHandler(IBaseRepository<GoodsslaughterInfo> slaughterbaseRepository, IIdContextCemil iidContextCemil)
        {
            _slaughterbaseRepository = slaughterbaseRepository;
            _iidContextCemil = iidContextCemil;
        }
        public async Task<APIResult<int>> Handle(GoodsslaughterAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                GoodsslaughterInfo info = new GoodsslaughterInfo();

                info.GoodsProductionbatches=request.GoodsProductionbatches;
                info.Id = YitIdHelper.NextId();
                info.Goodsslaughterweight = request.Goodsslaughterweight;
                info.GoodsslaughterComeTime = request.GoodsslaughterComeTime;
                info.GoodsslaughterleaveTime = request.GoodsslaughterleaveTime;
                info.GoodsId = request.GoodsId;
                info.IsDeleted = false;
                info.CreateName= _iidContextCemil.UserName;

                await _slaughterbaseRepository.CreateAsync(info);
                result.Code = ApiEnum.Success;
                result.Msg = "屠宰信息添加成功";
                result.Data = 1;
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "屠宰信息添加失败";
                return await Task.FromResult(result);
            }
        }
    }
}
